import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.ZAddParams;
import redis.clients.jedis.resps.Tuple;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * zadd zrange
 * zcard
 * zrem
 * zscore
 * zrank
 */

public class RedisZSet {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");

        try (Jedis jedis = jedisPool.getResource()) {
//            test1(jedis);

//            test2(jedis);

//            test3(jedis);

//            test4(jedis);

            test5(jedis);
        }
    }

    /**
     * zrank
     * @param jedis
     */
    public static void test5(Jedis jedis) {
        jedis.flushAll();

        Map<String, Double> map = new HashMap<>();
        map.put("aaa", 10.0);
        map.put("bbb", 6.0);
        map.put("ccc", 3.0);
        map.put("ddd", 7.0);
        map.put("eee", 9.0);

        jedis.zadd("key1", map);
        System.out.println(jedis.zrank("key1", "aaa"));
        System.out.println(jedis.zrank("key1", "bbb"));
        System.out.println(jedis.zrank("key1", "ccc"));
        System.out.println(jedis.zrank("key1", "ddd"));
        System.out.println(jedis.zrank("key1", "eee"));
        System.out.println(jedis.zrank("key1", "fff"));

    }

    /**
     * zscore
     * @param jedis
     */
    public static void test4(Jedis jedis) {
        jedis.flushAll();

        Map<String, Double> map = new HashMap<>();
        map.put("aaa", 10.0);
        map.put("bbb", 6.0);
        map.put("ccc", 3.0);
        map.put("ddd", 7.0);
        map.put("eee", 9.0);

        jedis.zadd("key1", map);

        System.out.println(jedis.zscore("key1", "aaa"));
        System.out.println(jedis.zscore("key1", "rrr"));

    }

    /**
     * zrem
     * @param jedis
     */
    public static void test3(Jedis jedis) {
        jedis.flushAll();

        Map<String, Double> map = new HashMap<>();
        map.put("aaa", 10.0);
        map.put("bbb", 6.0);
        map.put("ccc", 3.0);
        map.put("ddd", 7.0);
        map.put("eee", 9.0);

        jedis.zadd("key1", map);

        jedis.zrem("key1", "aaa", "bbb", "eee");
        List<Tuple> score_members = jedis.zrangeWithScores("key1", 0, -1); //同时获取 score 和 member
        for (Tuple score_member : score_members) {
            System.out.println("member: " + score_member.getElement() + ", score: " + score_member.getScore());
        }
    }

    /**
     * zcard
     * @param jedis
     */
    public static void test2(Jedis jedis) {
        jedis.flushAll();

        Map<String, Double> map = new HashMap<>();
        map.put("aaa", 10.0);
        map.put("bbb", 6.0);
        map.put("ccc", 3.0);
        map.put("ddd", 7.0);
        map.put("eee", 9.0);

        jedis.zadd("key1", map); //返回元素个数
        long count = jedis.zcard("key1");
        System.out.println(count);
    }

    /**
     * zadd
     * zrange
     * @param jedis
     */
    public static void test1(Jedis jedis) {
        jedis.flushAll();

        /**
         * key 为 member，map 的 value 为 score
         */
        Map<String, Double> map = new HashMap<>();
        map.put("aaa", 10.0);
        map.put("bbb", 6.0);
        map.put("ccc", 3.0);
        map.put("ddd", 7.0);
        map.put("eee", 9.0);

        jedis.zadd("key1", map);
        List<String> values = jedis.zrange("key1", 0, -1);
        System.out.println(values);

        ZAddParams zAddParams = new ZAddParams();
        zAddParams.nx();
        zAddParams.ch();

        long num = jedis.zadd("key1", 6.0, "fff", zAddParams);
        System.out.println(num);
        System.out.println(jedis.zrange("key1", 0, -1));
    }
}
